package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.constant.DeviceOptions;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_AlarmDataServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.collect.util.DateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 压力监测仪 压力监测仪异常事件上传
 * @author 48310
 *
 */
@Component
public class MessageProcessor55 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_AlarmDataServiceHelper device_alarmDataServiceHelper;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	DeviceOptions deviceOptions;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String responseCode = "0000";//响应码

		//查询物联网设备
		IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByIotPlatformDeviceId(deviceId);
		Integer iotDeviceId = iotDeviceModel.getId();

		//查询采集设备
		CollectDeviceModel collectDeviceModel = device_collectDeviceServiceHelper.getByIotDeviceId(iotDeviceId);
		Integer collectDeviceId = null;
		Integer gasUserId = null;
		if (collectDeviceModel != null) {
			collectDeviceId = collectDeviceModel.getId();
			gasUserId = collectDeviceModel.getGasUserId();
		}

		//如果报警数据不存在，则写入报警数据
		String currentDate = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE));
		String currentTime = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME));
		String alarmType = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.ALARM_TYPE));
		Date currentTm = DateUtil.strToDate(currentDate + currentTime, "yyyyMMddHHmmss");

		int existCount = device_alarmDataServiceHelper.getCountByIotDeviceIdAndCurrentTm(iotDeviceId, currentTm);
		if (existCount == 0) {
			AlarmDataPostModel alarmDataPostModel = new AlarmDataPostModel();
			alarmDataPostModel.setIotDeviceId(iotDeviceId);
			alarmDataPostModel.setCollectDeviceId(collectDeviceId);
			alarmDataPostModel.setGasUserId(gasUserId);
			alarmDataPostModel.setCurrentTm(currentTm.getTime());
			if ("0001".equals(alarmType)) {//欠压报警
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "QY"));
			} else if ("0002".equals(alarmType)) {//过压报警
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "GY"));
			} else if ("0004".equals(alarmType)) {//过热报警
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "GR"));
			}  else if ("0008".equals(alarmType)) {//存储异常
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "CCYC1"));
			} else if ("0010".equals(alarmType)) {//时钟异常
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "SZYC"));
			} else if ("0080".equals(alarmType)) {//低电报警
				alarmDataPostModel.setAlarmType(deviceOptions.getCode("ALARM_TYPE", "DDYC1"));
			}
			device_alarmDataServiceHelper.post(alarmDataPostModel);
		}

		//组装响应
		Map<MessageFieldKeyEnum, Object> responseObj = messageUtil.generatePressureMonitorResponse(protocolId, parsedMessageObj, responseCode, null);
		List<Map<MessageFieldKeyEnum, Object>> result = new ArrayList<>();
		result.add(responseObj);

		return result;
	}
}
